package com.collegeproject.weatherapp;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;

import com.collegeproject.weatherapp.fragments.DashboardFragment;
import com.collegeproject.weatherapp.fragments.EditFragment;
import com.collegeproject.weatherapp.fragments.LoginFragment;
import com.collegeproject.weatherapp.fragments.PassRecoveryFragment;
import com.collegeproject.weatherapp.fragments.PostFragment;
import com.collegeproject.weatherapp.fragments.PostsFragment;
import com.collegeproject.weatherapp.fragments.RegisterFragment;
import com.collegeproject.weatherapp.fragments.UsersFragment;
import com.collegeproject.weatherapp.fragments.WeatherFragment;


public class StartActivity extends FragmentActivity implements
		ActionBar.TabListener {

	private static final int NUM_TABS = 3;
	AppSectionsPagerAdapter mAppSectionsPagerAdapter;
	ViewPager mViewPager;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(
				getSupportFragmentManager());

		final ActionBar actionBar = getActionBar();

		// Specify that the Home/Up button should not be enabled, since there is
		// no hierarchical parent.

		// actionBar.setDisplayShowTitleEnabled(false);
		// actionBar.setDisplayShowHomeEnabled(false);
		actionBar.setHomeButtonEnabled(false);

		// Specify that we will be displaying tabs in the action bar.
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

		// Set up the ViewPager, attaching the adapter and setting up a listener
		// for when the user swipes between sections.
		mViewPager = (ViewPager) findViewById(R.id.pager);
		mViewPager.setAdapter(mAppSectionsPagerAdapter);
		mViewPager
				.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
					@Override
					public void onPageSelected(int position) {
						// When swiping between different app sections, select
						// the corresponding tab
						actionBar.setSelectedNavigationItem(position);
					}
				});

		for (int i = 0; i < mAppSectionsPagerAdapter.getCount(); i++) {
			// Create tabs
			actionBar.addTab(actionBar.newTab()
					.setText(mAppSectionsPagerAdapter.getPageTitle(i))
					.setTabListener(this));
		}
		mViewPager.setOffscreenPageLimit(3);

	}

	@Override
	public void onBackPressed() {
		if (mViewPager.getCurrentItem() == 0) {
			// If the user is currently looking at the first step, allow the
			// system to handle the
			// Back button. This calls finish() on this activity and pops the
			// back stack.
			super.onBackPressed();
		} else {
			// Otherwise, select the previous step.
			mViewPager.setCurrentItem(mViewPager.getCurrentItem() - 1);
		}
	}

	// custom Fragment Pager Adapter
	public static class AppSectionsPagerAdapter extends FragmentPagerAdapter {

		private final class FragmentListenerClass implements FragmentListener {

			// fragment listener interface method
			public void onSwitchToNextFragment() {
				mFragmentManager.beginTransaction().remove(mFragmentAtPos3)
						.commit();
				if (mFragmentAtPos3 instanceof LoginFragment) {
					if (FragmentToChangeTo == 5) {
						mFragmentAtPos3 = RegisterFragment
								.newInstance(listener);
					} else if (FragmentToChangeTo == 6) {
						mFragmentAtPos3 = PassRecoveryFragment
								.newInstance(listener);
					} else {
						mFragmentAtPos3 = DashboardFragment
								.newInstance(listener);
					}
				} else if (mFragmentAtPos3 instanceof DashboardFragment) {
					if (FragmentToChangeTo == 1) {
						mFragmentAtPos3 = LoginFragment.newInstance(listener);
					} else if (FragmentToChangeTo == 2) {
						mFragmentAtPos3 = PostFragment.newInstance(listener);
					} else if (FragmentToChangeTo == 3) {
						mFragmentAtPos3 = EditFragment.newInstance(listener);
					} else if (FragmentToChangeTo == 4) {
						mFragmentAtPos3 = UsersFragment.newInstance(listener);
					}
				} else if (mFragmentAtPos3 instanceof PassRecoveryFragment) {
					mFragmentAtPos3 = LoginFragment.newInstance(listener);
				} else if (mFragmentAtPos3 instanceof RegisterFragment) {
					mFragmentAtPos3 = LoginFragment.newInstance(listener);
				} else if (mFragmentAtPos3 instanceof PostFragment
						|| mFragmentAtPos3 instanceof EditFragment
						|| mFragmentAtPos3 instanceof UsersFragment) {
					mFragmentAtPos3 = DashboardFragment.newInstance(listener);
				}
				notifyDataSetChanged();
			}

		}

		public static int FragmentToChangeTo;
		FragmentListenerClass listener = new FragmentListenerClass();
		private Fragment mFragmentAtPos1, mFragmentAtPos2, mFragmentAtPos3;
		private final FragmentManager mFragmentManager;

		public AppSectionsPagerAdapter(FragmentManager fm) {
			super(fm);
			mFragmentManager = fm;
		}

		// return fragment
		@Override
		public Fragment getItem(int i) {
			switch (i) {
			case 0:
				if (mFragmentAtPos1 == null) {
					mFragmentAtPos1 = WeatherFragment.newInstance(listener);
				}
				return mFragmentAtPos1;
			case 1:
				if (mFragmentAtPos2 == null) {
					mFragmentAtPos2 = PostsFragment.newInstance(listener);
				}
				return mFragmentAtPos2;
			default:
				if (mFragmentAtPos3 == null) {
					mFragmentAtPos3 = LoginFragment.newInstance(listener);
				}
				return mFragmentAtPos3;
			}
		}

		// total tabs
		@Override
		public int getCount() {
			return NUM_TABS;
		}

		// title of tabs
		@Override
		public CharSequence getPageTitle(int position) {
			if (position == 0) {
				return "Weather";
			} else if (position == 1) {
				return "Comments";
			} else {
				return "Dashboard";
			}
		}

		// replacing current visible fragment after fragment transaction with
		// the desired one and notify view pager
		@Override
		public int getItemPosition(Object object) {
			if (object instanceof WeatherFragment
					&& mFragmentAtPos1 instanceof WeatherFragment)
				return POSITION_NONE;
			if (object instanceof PostsFragment
					&& mFragmentAtPos2 instanceof PostsFragment)
				return POSITION_NONE;
			if (object instanceof LoginFragment
					&& mFragmentAtPos3 instanceof DashboardFragment
					|| object instanceof LoginFragment
					&& mFragmentAtPos3 instanceof RegisterFragment
					|| object instanceof LoginFragment
					&& mFragmentAtPos3 instanceof PassRecoveryFragment)
				return POSITION_NONE;
			if (object instanceof PassRecoveryFragment
					&& mFragmentAtPos3 instanceof LoginFragment)
				return POSITION_NONE;
			if (object instanceof RegisterFragment
					&& mFragmentAtPos3 instanceof LoginFragment
					|| object instanceof RegisterFragment
					&& mFragmentAtPos3 instanceof DashboardFragment)
				return POSITION_NONE;
			if (object instanceof DashboardFragment
					&& mFragmentAtPos3 instanceof LoginFragment
					|| object instanceof DashboardFragment
					&& mFragmentAtPos3 instanceof PostFragment
					|| object instanceof DashboardFragment
					&& mFragmentAtPos3 instanceof EditFragment
					|| object instanceof DashboardFragment
					&& mFragmentAtPos3 instanceof UsersFragment)
				return POSITION_NONE;
			if (object instanceof PostFragment
					&& mFragmentAtPos3 instanceof DashboardFragment)
				return POSITION_NONE;
			if (object instanceof EditFragment
					&& mFragmentAtPos3 instanceof DashboardFragment)
				return POSITION_NONE;
			if (object instanceof UsersFragment
					&& mFragmentAtPos3 instanceof DashboardFragment)
				return POSITION_NONE;
			return POSITION_UNCHANGED;
		}
	}

	public interface FragmentListener {
		void onSwitchToNextFragment();

	}

	@Override
	public void onTabReselected(Tab tab, android.app.FragmentTransaction ft) {

	}

	@Override
	public void onTabSelected(Tab tab, android.app.FragmentTransaction ft) {
		mViewPager.setCurrentItem(tab.getPosition());
	}

	@Override
	public void onTabUnselected(Tab tab, android.app.FragmentTransaction ft) {

	}

}
